package com.shujia.flink.window

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.ProcessingTimeSessionWindows
import org.apache.flink.streaming.api.windowing.time.Time

object Demo3SessionWindow {
  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    val kvDS: DataStream[(String, Int)] = linesDS.flatMap(_.split(",")).map((_, 1))

    /**
     * 会话窗口，一段时间没有数据将前面的数据划分为一个窗口，进行计算，每个key独立计时的
     * ProcessingTimeSessionWindows:处理时间的会话窗口
     * EventTimeSessionWindows：事件时间的会话窗口
     */
    kvDS
      .keyBy(_._1)
      .window(ProcessingTimeSessionWindows.withGap(Time.seconds(5)))
      .sum(1)
      .print()

    env.execute()


  }

}
